<template>
  <div class="app-container">
    <el-form class="formList"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="88px">
      <el-row>

        <el-form-item label="领取人"
          prop="customerName">
          <el-input v-model="queryParams.customerName"
            placeholder="请输入领取人"
            clearable
            size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="优惠券名称"
          prop="couponName">
          <el-input v-model="queryParams.couponName"
            placeholder="请输入优惠券名称"
            clearable
            size="small"
            @keyup.enter.native="handlequery" />
        </el-form-item>

        <el-form-item label="优惠券类型"
          prop="couponType">
          <el-select v-model="queryParams.couponType"
            placeholder="请选择优惠券类型"
            clearable
            size="small">
            <el-option v-for="dict in couponTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

      </el-row>
      <el-row>

        <el-form-item label="优惠券来源"
          prop="couponSource">
          <el-select v-model="queryParams.couponSource"
            placeholder="请选择优惠券来源"
            clearable
            size="small">
            <el-option v-for="dict in couponSourceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="使用状态"
          prop="isUsed">
          <el-select v-model="queryParams.isUsed"
            placeholder="请选择使用状态"
            clearable
            size="small">
            <el-option v-for="dict in isUsedOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="门店"
          prop="storeId">
          <el-select v-model="queryParams.storeId"
            placeholder="请选择门店"
            clearable


            size="small"
            @keyup.enter.native="handleQuery">
            <el-option v-for="dict in storeInFoOptions"
              :key="dict.storeId"
              :label="dict.storeName"
              :value="dict.storeUid" />
          </el-select>
        </el-form-item>
        <!--
        <el-form-item label="数据状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择数据状态" clearable size="small">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item> -->
        <el-form-item style="margin-left: 6px;">
          <el-button type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"
            size="mini"
            @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
      <!-- <el-row>
        <el-form-item style="margin-left: 22px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row> -->
    </el-form>

    <el-row class="row-mbinfo">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['activity:couponDetail:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['activity:couponDetail:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['activity:couponDetail:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:couponDetail:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"
        @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table border
        v-loading="loading"
        :data="couponDetailList"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection"
          width="55"
          align="center" />
          <el-table-column label="用户头像"
            align="center"
            prop="customerUrl">
            <template slot-scope="scope">
              <el-image v-if="scope.row.customerUrl != null && scope.row.customerUrl != '' "
                slot="reference"
                :src="scope.row.customerUrl"
                class="image-rows"
                :preview-src-list="[scope.row.customerUrl]">
              </el-image>
            </template>
          </el-table-column>
        <el-table-column label="用户昵称"
          align="center"
          prop="customerName"
          width="120"
          :show-overflow-tooltip="true" />

        <!--
        <el-table-column label="是否转赠"
          align="center"
          prop="isTransferStr"
          :show-overflow-tooltip="true"
          width="100" />
        <el-table-column label="优惠券来源"
          align="center"
          prop="couponSource"
          :formatter="couponSourceFormat"
          width="100" />
          -->
        <el-table-column label="类型"
            align="center"
            prop="couponType"
            :formatter="couponTypeFormat"
            width="80" />
        <el-table-column label="优惠券名称"
          min-width="180"
          align="center"
          prop="couponName"
          :show-overflow-tooltip="true"
          width="180" />

        <el-table-column label="优惠内容"
          align="center"
          prop="couponAmount"
          :show-overflow-tooltip="true"
          width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.couponAmountStr }}</span>
          </template>
        </el-table-column>
        <el-table-column label="门店"
          min-width="150"
          align="center"
          prop="storeName"
          />
        <el-table-column label="状态"
          align="center"
          prop="dataStatus"
          :formatter="dataStatusFormat" />
        <el-table-column label="使用状态"
          align="center"
          prop="isUsed"
          :formatter="isUsedFormat" />
        <el-table-column label="生效时间"
          align="center"
          prop="couponStartDate"
          width="170"
          sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.couponStartDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="到期时间"
          align="center"
          prop="couponEndDate"
          width="170"
          sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.couponEndDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="使用时间"
          align="center"
          prop="usedTime"
          width="170"
          sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.usedTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="获得时间"
          align="center"
          prop="createTime"
          width="170"
          sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">          
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            >删除</el-button>
        </template>
      </el-table-column> 
      </el-table>

      <pagination v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改用户优惠券领取信息对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      @keyup.enter.native="submitForm">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="80px">
        <el-form-item label="领取人ID"
          prop="customerUid">
          <el-input v-model="form.customerUid"
            placeholder="请输入领取人ID" />
        </el-form-item>
        <el-form-item label="领取人"
          prop="customerName">
          <el-input v-model="form.customerName"
            placeholder="请输入领取人" />
        </el-form-item>
        <el-form-item label="领取人头像"
          prop="customerUrl">
          <el-input v-model="form.customerUrl"
            placeholder="请输入领取人头像" />
        </el-form-item>
        <el-form-item label="优惠券ID"
          prop="couponUid">
          <el-input v-model="form.couponUid"
            placeholder="请输入优惠券ID" />
        </el-form-item>
        <el-form-item label="优惠券平台ID"
          prop="platformUid">
          <el-input v-model="form.platformUid"
            placeholder="请输入优惠券平台ID" />
        </el-form-item>
        <el-form-item label="优惠券名称"
          prop="couponName">
          <el-input v-model="form.couponName"
            placeholder="请输入优惠券名称" />
        </el-form-item>
        <el-form-item label="优惠券类型"
          prop="couponType">
          <el-select v-model="form.couponType"
            placeholder="请选择优惠券类型">
            <el-option v-for="dict in couponTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券图标"
          prop="couponIconUrl">
          <el-input v-model="form.couponIconUrl"
            placeholder="请输入优惠券图标" />
        </el-form-item>
        <el-form-item label="优惠金额/折扣率"
          prop="couponAmount">
          <el-input v-model="form.couponAmount"
            placeholder="请输入优惠金额/折扣率" />
        </el-form-item>
        <el-form-item label="使用门槛"
          prop="baseAmount">
          <el-input v-model="form.baseAmount"
            placeholder="请输入使用门槛" />
        </el-form-item>
        <el-form-item label="生效时间"
          prop="couponStartDate">
          <el-date-picker clearable
            size="small"
            v-model="form.couponStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间"
          prop="couponEndDate">
          <el-date-picker clearable
            size="small"
            v-model="form.couponEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="描述"
          prop="couponDesc">
          <el-input v-model="form.couponDesc"
            placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="数据状态"
          prop="dataStatus">
          <el-select v-model="form.dataStatus"
            placeholder="请选择数据状态">
            <el-option v-for="dict in dataStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态"
          prop="isUsed">
          <el-input v-model="form.isUsed"
            placeholder="请输入使用状态" />
        </el-form-item>
        <el-form-item label="使用时间"
          prop="usedTime">
          <el-date-picker clearable
            size="small"
            v-model="form.usedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="优惠券来源"
          prop="couponSource">
          <el-select v-model="form.couponSource"
            placeholder="请选择优惠券来源">
            <el-option v-for="dict in couponSourceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看用户优惠券领取信息对话框 -->
    <el-dialog :title="viewTitle"
      :visible.sync="viewOpen"
      width="700px"
      append-to-body>
      <el-form ref="detailForm"
        label-width="100px">
        <el-row>
          <el-col :span="12"
            v-for="(item, index) in showData"
            :key="index"
            style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          size="mini"
          @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  selectGroup
} from "@/api/platform/store"
import {
  listCouponDetail,
  getCouponDetail,
  delCouponDetail,
  addCouponDetail,
  updateCouponDetail
} from "@/api/activity/couponDetail";

export default {
  name: "CouponDetail",
  components: {},
  data () {
    return {

      storeInFoOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户优惠券领取信息表格数据
      couponDetailList: [],
      // 弹出层标题
      title: "",
      //查看详细标题
      viewTitle: "",
      // 是否显示弹出层
      open: false,
      //是否显示查看详细弹出层
      viewOpen: false,
      //查看详细数据
      showData: [],
      // 优惠券类型字典
      couponTypeOptions: [],
      // 数据状态字典
      dataStatusOptions: [],
      // 使用状态字典
      isUsedOptions: [],
      // 优惠券来源字典
      couponSourceOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        couponName: null,
        couponType: null,
        couponStartDate: null,
        couponEndDate: null,
        dataStatus: null,
        isUsed: null,
        usedTime: null,
        createTime: null,
        couponSource: null,
        storeId:''
      },
      columns: [{
        label: 'ID',
        dataIndex: 'detailUid',
      },
      {
        label: '领取人ID',
        dataIndex: 'customerUid',
      },
      {
        label: '领取人',
        dataIndex: 'customerName',
      },
      {
        label: '领取人头像',
        dataIndex: 'customerUrl',
      },
      {
        label: '优惠券ID',
        dataIndex: 'couponUid',
      },
      {
        label: '优惠券平台ID',
        dataIndex: 'platformUid',
      },
      {
        label: '优惠券名称',
        dataIndex: 'couponName',
      },
      {
        label: '优惠券类型',
        dataIndex: 'couponType',
      },
      {
        label: '优惠券图标',
        dataIndex: 'couponIconUrl',
      },
      {
        label: '优惠金额/折扣率',
        dataIndex: 'couponAmount',
      },
      {
        label: '使用门槛',
        dataIndex: 'baseAmount',
      },
      {
        label: '生效时间',
        dataIndex: 'couponStartDate',
      },
      {
        label: '到期时间',
        dataIndex: 'couponEndDate',
      },
      {
        label: '描述',
        dataIndex: 'couponDesc',
      },
      {
        label: '数据状态',
        dataIndex: 'dataStatus',
      },
      {
        label: '使用状态',
        dataIndex: 'isUsed',
      },
      {
        label: '使用时间',
        dataIndex: 'usedTime',
      },
      {
        label: '获得时间',
        dataIndex: 'createTime',
      },
      {
        label: '优惠券来源',
        dataIndex: 'couponSource',
      },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created () {
    this.getList();
    this.getDicts("activity_coupon_type").then(response => {
      this.couponTypeOptions = response.data;
    });
    this.getDicts("sys_data_status").then(response => {
      this.dataStatusOptions = response.data;
    });
    this.getDicts("activity_coupon_use").then(response => {
      this.isUsedOptions = response.data;
    });
    this.getDicts("activity_coupon_source").then(response => {
      this.couponSourceOptions = response.data;
    });
    selectGroup().then(response => {
      this.storeInFoOptions = response.data || [];
    });
  },
  methods: {
    /** 查询用户优惠券领取信息列表 */
    getList () {
      this.loading = true;
      listCouponDetail(this.queryParams).then(response => {
        this.couponDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 优惠券类型字典翻译
    couponTypeFormat (row, column) {
      return this.selectDictLabel(this.couponTypeOptions, row.couponType);
    },
    // 数据状态字典翻译
    dataStatusFormat (row, column) {
      return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
    },
    // 使用状态字典翻译
    isUsedFormat (row, column) {
      return this.selectDictLabel(this.isUsedOptions, row.isUsed);
    },
    // 优惠券来源字典翻译
    couponSourceFormat (row, column) {
      return this.selectDictLabel(this.couponSourceOptions, row.couponSource);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        detailUid: null,
        customerUid: null,
        customerName: null,
        customerUrl: null,
        couponUid: null,
        platformUid: null,
        couponName: null,
        couponType: null,
        couponIconUrl: null,
        couponAmount: null,
        baseAmount: null,
        couponStartDate: null,
        couponEndDate: null,
        couponDesc: null,
        dataStatus: null,
        isUsed: null,
        usedTime: null,
        createTime: null,
        couponSource: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.detailUid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加用户优惠券领取信息";
    },
    /** 查看详情按钮操作 **/
    handleShowDetail (row) {
      this.viewOpen = true;
      this.viewTitle = "查看用户优惠券领取信息";
      this.showData = [];
      this.columns.forEach(item => {
        this.showData.push({
          label: item.label,
          value: row[item.dataIndex]
        })
      })
    },
    handCloseDetail () {
      this.viewOpen = false
      this.showData = []
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const detailUid = row.detailUid || this.ids
      getCouponDetail(detailUid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户优惠券领取信息";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.detailUid != null) {
            updateCouponDetail(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCouponDetail(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const detailUids = row.detailUid || this.ids;
      this.$confirm('是否确认删除该优惠券?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delCouponDetail(detailUids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('prod-api/activity/couponDetail/export', {
        ...this.queryParams
      }, `activity_couponDetail.xlsx`)
    }
  }
};
</script>
